<?php
/**
 * Created by PhpStorm.
 * User: LAKECASEY
 * Date: 2018/12/10
 * Time: 13:48
 */
namespace App\Http\Controllers\Web;
use App\Http\Controllers\Controller;
use App\model\Misspeople;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use App\model\China;
use Illuminate\Support\Facades\DB;
class FindController extends Controller
{
    /**
     * 发布寻人
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\think\response\View
     */
    public function index()
    {
        //SEO
        $SEO = config('seo.find');
        $title = $SEO['title'];
        $keywords = $SEO['keywords'];
        $description = $SEO['description'];
        $active = 1;
        //查询所有地区
        $areaname = new China();
        $areaarray = $areaname->getAllarea();
        //获取编码
        $code_nums = $this->CodeNumber();
        //判断是否是移动端
        $isMobile= $this->isMobile();
        if ($isMobile)
        {
            $active = 7;
            return view('wap.find.index',compact('title','keywords','description','active','areaarray','code_nums'));
        }
        else
        {
            return view('web.find.index',compact('title','keywords','description','active','areaarray','code_nums'));
        }

    }

    /**
     * 发布寻人处理
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\think\response\Redirect
     */
    public function addmsg(Request $request)
    {
        $SEO = config('seo.find');
        $title = $SEO['title'];
        $keywords = $SEO['keywords'];
        $description = $SEO['description'];
        //接收所有的参数
        if($request -> hasFile('miss_pic')){
            // 使用request 创建文件上传对象
            $file = $request -> file('miss_pic');
            // 获取文件相关信息
            $originalName = $file->getClientOriginalName(); // 文件原名
            $ext = $file->getClientOriginalExtension();     // 扩展名
            $realPath = $file->getRealPath();   //临时文件的绝对路径
            $type = $file->getClientMimeType();     // image/jpeg

            // 上传文件
            $filename = date('Y-m-d-H-i-s') . '-' . uniqid() . '.' . $ext;
            // 使用我们新建的uploads本地存储空间（目录）
            //这里的uploads是配置文件的名称
            $bool = Storage::disk('uploads')->put($filename, file_get_contents($realPath));
            $request['peope_pic'] = $filename;
            $model = new Misspeople();
            $allres = $model->Addpeople($request);

            if ($allres)
            {
                return redirect('/');
            }
        }
    }

    /**
     * 寻人大厅
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\think\response\View
     */
    public function xrlist(Request $request)
    {
        $SEO = config('seo.findlist');
        $title = $SEO['title'];
        $keywords = $SEO['keywords'];
        $description = $SEO['description'];
        $active = 2;//寻人大厅
        $typeid = isset($request) ? $request->typeid : 0;
        //默认显示数据
        $db = new Misspeople();
        $defaultinfos = $db->getAlldatas($typeid,$request);
        //检查是否是移动端
        $is_mobile = $this->isMobile();
        if ($is_mobile)
        {
            return view('wap.find.list',compact('title','keywords','description','active','typeid','defaultinfos'));
        }
        else
        {
            return view('web.find.list',compact('title','keywords','description','active','typeid','defaultinfos'));
        }

    }

    /**
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\think\response\View
     */
    public function search(Request $request)
    {
        $SEO = config('seo.findlist');
        $title = $SEO['title'];
        $keywords = $SEO['keywords'];
        $description = $SEO['description'];
        $active = 2;//寻人大厅
        $typeid = isset($request) ? $request['typeid']: 0;
        $search_text = isset($request['search_text']) ? $request['search_text'] :'' ;
        //区分来源 1电脑端2移动端
        $server = isset($request['servertype']) ? $request['servertype'] :1;
        //通过php正则匹配，将中文标点替换成英文标点
        $search_text = preg_replace("/(，)/" ,',' ,$search_text);
        $search_array = explode(',',$search_text);
        //1、查询数据库总条数
        $count = count(Db::table('misspeople')->get());
        //2、设置每页显示条数
        $rev = '8';
        //3、求总页数
        $sums = ceil($count/$rev);
        //4、求单前页
        $page = $request['page'];
        if(empty($page)){
            $page = "1";
        }
        //5、设置上一页、下一页
        $prev = ($page-1)>0?$page-1:1;
        $next = ($page+1)<$sums?$page+1:$sums;
        //6、求偏移量
        $offset = ($page-1)*$rev;
        //7、sql查询数据库
        $select_s ='';
        $typeslect = '';
        $search = '';
        if (!empty($search_array))
        {
            if (!empty($search_array[0]))
            {
               $select_s = "and misspeople.miss_name like '%$search_array[0]%'";
            }
            elseif (!empty($search_array[0]) && !empty($search_array[1]))
            {
                $select_s = "and misspeople.miss_name like '%$search_array[0]%' and misspeople.miss_sex like '%$search_array[1]%'";
            }
            elseif (!empty($search_array[0]) && !empty($search_array[1]) && !empty($search_array[2]))
            {
                $select_s = "and misspeople.miss_name like '%$search_array[0]%' and misspeople.miss_sex like '%$search_array[1]%' and misspeople.miss_access like '%$search_array[2]%'";
            }
        }
        if (!empty($_COOKIE['citycode']) && is_numeric($_COOKIE['citycode']))
        {
            $search = "and misspeople.find_area =".$_COOKIE['citycode'];
        }
        if (!empty($typeid))
        {
            $typeslect = "and find_type=$typeid";
        }
        $data = Db::select("select misspeople.*,china.Name from misspeople left join  china on (misspeople.find_area = china.Id) where misspeople.is_shelf=1 $search $select_s  $typeslect limit $offset,$rev");
        //8、数字分页(可有可无)
        $pp = array();
        for($i=1;$i<=$sums;$i++){
            $pp[$i]=$i;
        }
        if ($server == 2)
        {
            return view('wap.find.search',compact('title','keywords','description','active','typeid','data','prev','next','sums','page','pp'));
        }
        else
        {
            return view('web.find.search',compact('title','keywords','description','active','typeid','data','prev','next','sums','page','pp'));die;
        }
    }

    /**
     * 详情
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\think\response\View
     */
    public function detilas(Request $request)
    {
        //获取id
        $t_id = $request->id;
        $db = new Misspeople();
        $detial = $db->getDetial($t_id);
        //seo配置文件
        $SEO = config('seo.finddetial');
        $title = sprintf($SEO['title'],$detial->miss_name);
        $keywords = sprintf($SEO['keywords'],$detial->miss_face_text);
        $description = sprintf($SEO['description'],$detial->miss_experience);
        $active = 2;//寻人大厅
        //推荐出相似的信息
        $same_data = $db->getSame($t_id,$detial->find_type);
        //判断是否是移动端
        $isMobile = $this->isMobile();
        if ($isMobile)
        {
            $active = '2-1';
            return view('wap.find.detial',compact('title','keywords','description','active','detial','same_data'));
        }
        else
        {
            return view('web.find.detial',compact('title','keywords','description','active','detial','same_data'));
        }

    }

    /**
     * 首页寻人大厅切换
     * @param Request $request
     * @return string
     */
    public function changetab(Request $request)
    {
        if (!empty($request))
        {
            //1代表电脑端2手机端
            $service = isset($request['servertype']) ? $request['servertype'] :1;
            $db= new Misspeople();
            if ($request['typeid'] != 'all')
            {
                $select_tab = $db->findtype_msg($request['typeid']);
            }
            else
            {
                $select_tab = $db->findtype_msg_all();
            }
            if (!empty($select_tab))
            {
                if ($service == 1)
                {
                    $html ='';
                    foreach ($select_tab as $v)
                    {
                        $html.="<li class=\"fl\"><a href=\"/xrdt/$v->id.html\"><img style=\"width:114px;height: 150px\" class=\"specical_img\" src=\"img/$v->miss_pic\" alt=\"\"/><h1>$v->miss_name   $v->Name </h1></a></li>";

                    }
                    return $html;
                }
                else
                {
                    $html = '';
                    foreach ($select_tab as $v)
                    {
                        $html.="<li class=\"fl\"><a href=\"/xrdt/$v->id.html\"><img style=\"width:114px;height: 150px\" class=\"specical_img\" src=\"img/$v->miss_pic\" alt=\"\"/><h1>$v->miss_name   $v->Name </h1></a></li>";
                    }
                    return $html;
                }
            }
            else
            {
                return "<li><h1></h1></li>";
            }
        }
    }
}